package com.lez.kotlincoroutine.api

import android.util.Log
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET

data class Banner(val id: Int,val imagePath: String,val title: String,val url: String)
data class ResponseBanner(val errorCode: Int,val errorMsg: String,val data: List<Banner>)

val userServiceApi: UserServiceApi by lazy {

    //构建网络请求
    val retrofit = Retrofit.Builder()
        .baseUrl("https://www.wanandroid.com")
        .addConverterFactory(MoshiConverterFactory.create())
        //添加网络请求拦截器
        .client(OkHttpClient.Builder().addInterceptor{
            it.proceed(it.request()).apply {
                Log.i("info","request:${code()}")
            }
        }.build())
        .build()

    retrofit.create(UserServiceApi::class.java)
}


interface UserServiceApi {

    @GET("/banner/json")
    fun loadBanner(): Call<ResponseBanner>

    //协程版网络请求
    @GET("/banner/json")
    suspend fun getBanner(): ResponseBanner

}